package com.css.openapi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.css.common.core.constant.Result;
import com.css.common.core.exception.ServiceException;
import com.css.common.core.session.SessionUtils;
import com.css.common.core.utils.BaseUtil;
import com.css.common.core.utils.GyUtils;
import com.css.openapi.BO.*;
import com.css.openapi.BO.fxdw.FxOrderCancelBO;
import com.css.openapi.BO.fxdw.FxOrderQueryBO;
import com.css.openapi.BO.fxdw.FxkpOrderBO;
import com.css.openapi.BO.fxdw.FxkpOrderResultBO;
import com.css.openapi.BO.fxdw.PayAndKpSubmitBO;
import com.css.openapi.BO.fxdw.SfmxBO;
import com.css.openapi.BO.fxdw.YwddmxBO;
import com.css.openapi.BO.fxdw.ZfpzxxBO;
import com.css.openapi.entity.YhZrrxx;
import com.css.openapi.entity.ZfjkpFxkpDdmxbDO;
import com.css.openapi.entity.ZfjkpFxkpDdxxbDO;
import com.css.openapi.entity.ZfjkpFxkpSkxxbDO;
import com.css.openapi.entity.ZfjkpFxkpZfpzxxbDO;
import com.css.openapi.feign.ZfjkpPtCommonClient;
import com.css.openapi.mapper.ZfjkpFxkpDdxxbMapper;
import com.css.openapi.service.YhZrrxxService;
import com.css.openapi.service.ZfjkpFxkpDdmxbService;
import com.css.openapi.service.ZfjkpFxkpDdxxbService;
import com.css.openapi.service.ZfjkpFxkpSkxxbService;
import com.css.openapi.service.ZfjkpFxkpZfpzxxbService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* @author yuanyuan
* @description 针对表【zfjkp_fxkp_ddxxb(订单信息表)】的数据库操作Service实现
* @createDate 2025-10-10 16:33:44
*/
@Service
public class ZfjkpFxkpDdxxbServiceImpl extends ServiceImpl<ZfjkpFxkpDdxxbMapper, ZfjkpFxkpDdxxbDO>
    implements ZfjkpFxkpDdxxbService{

    @Resource
    private ZfjkpPtCommonClient zfjkpPtCommonClient;

    @Resource
    private ZfjkpFxkpDdmxbService zfjkpFxkpDdmxbService;

    @Resource
    private ZfjkpFxkpSkxxbService zfjkpFxkpSkxxbService;

    @Resource
    private ZfjkpFxkpZfpzxxbService zfjkpFxkpZfpzxxbService;

    @Resource
    private YhZrrxxService yhZrrxxService;

    @Override
    @Transactional
    public FxkpOrderResultBO createOrder(FxkpOrderBO fxkpOrderBO) {
        //调用支付即开票平台
        FxkpOrderResultBO bo= zfjkpPtCommonClient.createOrder(fxkpOrderBO);
        if(!Objects.equals(bo.getCode(), "0"))throw new ServiceException(bo.getMsg());
        String appid=SessionUtils.getAppid();
        String dduuid = GyUtils.getUuid();
        List<YwddmxBO> ywddmx = fxkpOrderBO.getYwddmx();
        for (YwddmxBO ywddmxBO : ywddmx) {
            zfjkpFxkpDdmxbService.saveOrUpdate(new ZfjkpFxkpDdmxbDO()
                    .setUuid(GyUtils.getUuid())
                    .setDduuid(dduuid)
                    .setXmmc(ywddmxBO.getXmmc())
                    .setSphfwssflhbbm(ywddmxBO.getSphfwssflhbbm())
                    .setGgxh(ywddmxBO.getGgxh())
                    .setDw(ywddmxBO.getDw())
                    .setSl(ywddmxBO.getSl())
                    .setDj(ywddmxBO.getDj())
                    .setSlv(ywddmxBO.getSlv())
                    .setJe(ywddmxBO.getJe())
                    .setCjr(appid)
                    .setCjsj(new Date())
            );
        }
        List<SfmxBO> sfmx= bo.getSkxx();
        for (SfmxBO sfmxBO : sfmx) {
            zfjkpFxkpSkxxbService.saveOrUpdate(new ZfjkpFxkpSkxxbDO()
                    .setUuid(GyUtils.getUuid())
                    .setDduuid(dduuid)
                    .setYbtse(sfmxBO.getYbtse())
                    .setZsxmDm(sfmxBO.getZsxmDm())
                    .setZspmDm(sfmxBO.getZspmDm())
                    .setZszmDm(sfmxBO.getZszmDm())
                    .setSkssqq(sfmxBO.getSkssqq())
                    .setSkssqz(sfmxBO.getSkssqz())
                    .setCjr(appid)
                    .setCjsj(new Date())
            );
        }
        this.saveOrUpdate(new ZfjkpFxkpDdxxbDO()
                .setUuid(dduuid)
                .setShbdbh(fxkpOrderBO.getShbdbh())
                .setKpybdbh(fxkpOrderBO.getKpybdbh())
                .setYwlx(fxkpOrderBO.getYwlx())
                .setFppzDm(fxkpOrderBO.getFppzDm())
                .setYwlsh(fxkpOrderBO.getYwlsh())
                .setZfkpqrfs(fxkpOrderBO.getZfkpqrfs())
                .setZrrxm(fxkpOrderBO.getZrrxm())
                .setZrrsfzjhm(fxkpOrderBO.getZrrsfzjhm())
                .setZrrsjhm(fxkpOrderBO.getZrrsjhm())
                .setZrryhzh(fxkpOrderBO.getZrryhzh())
                .setKhyhszsf(fxkpOrderBO.getKhyhszsf())
                .setJazslxDm(fxkpOrderBO.getJazslxDm())
                .setSfcdf(fxkpOrderBO.getSfcdf())
                .setFpbz(fxkpOrderBO.getFpbz())
                .setKptzhddz(fxkpOrderBO.getKptzhddz())
                .setZftzhddz(fxkpOrderBO.getZftzhddz())
                .setDdbh(bo.getDdbh())
                .setDdzt(bo.getDdzt())
                .setDescription(bo.getDescription())
                .setCjr(appid)
                .setCjsj(new Date()));
        return bo;
    }

    @Override
    @Transactional
    public Map<String, String> createFxOrderBatch(PcBO pc) {
//调用支付即开票平台
        Result<Map<String,String>> result= zfjkpPtCommonClient.batchCreateOrder(pc);
        if(!Objects.equals(result.getCode(), "0"))throw new ServiceException(result.getMsg());
        Map<String,String> map= result.getData();
        String appid=SessionUtils.getAppid();
        List<PcOrderBO> fxkpOrder = pc.getDdmx();
        for (PcOrderBO fxkpOrderBO : fxkpOrder) {
            String dduuid = GyUtils.getUuid();
            ZfpzxxBO zfpzxxBO= fxkpOrderBO.getZfpzxx();
            zfjkpFxkpZfpzxxbService.saveOrUpdate(new ZfjkpFxkpZfpzxxbDO()
                    .setUuid(GyUtils.getUuid())
                    .setDduuid(dduuid)
                    .setJylsh(zfpzxxBO.getJylsh())
                    .setJyrq(zfpzxxBO.getJyrq())
                    .setPcjylsh(zfpzxxBO.getPcjylsh())
                    .setZzje(zfpzxxBO.getZzje())
                    .setFkfyhbm(zfpzxxBO.getFkfyhbm())
                    .setFkfyhmc(zfpzxxBO.getFkfyhmc())
                    .setFkfyhzh(zfpzxxBO.getFkfyhzh())
                    .setSkfyhmc(zfpzxxBO.getSkfyhmc())
                    .setSkfyhzh(zfpzxxBO.getSkfyhzh())
                    .setCjr(appid)
                    .setCjsj(new Date())
            );
            List<PcOrderDetailBO> ywddmx = fxkpOrderBO.getYwddmx();
            for (PcOrderDetailBO ywddmxBO : ywddmx) {
                zfjkpFxkpDdmxbService.saveOrUpdate(new ZfjkpFxkpDdmxbDO()
                        .setUuid(GyUtils.getUuid())
                        .setDduuid(dduuid)
                        .setXmmc(ywddmxBO.getXmmc())
                        .setSphfwssflhbbm(ywddmxBO.getSphfwssflhbbm())
                        .setGgxh(ywddmxBO.getGgxh())
                        .setDw(ywddmxBO.getDw())
                        .setSl(ywddmxBO.getSl())
                        .setDj(ywddmxBO.getDj())
                        .setSlv(ywddmxBO.getSlv())
                        .setJe(ywddmxBO.getJe())
                        .setCjr(appid)
                        .setCjsj(new Date())
                );
            }
            this.saveOrUpdate(new ZfjkpFxkpDdxxbDO()
                    .setUuid(dduuid)
                    .setPch(pc.getPch())
                    .setPlcjddjgtzhddz(pc.getPlcjddjgtzhddz())
                    .setSllsh(map.get("sllsh"))
                    .setShbdbh(fxkpOrderBO.getShbdbh())
                    .setKpybdbh(fxkpOrderBO.getKpybdbh())
                    .setYwlx(pc.getYwlx())
                    .setFppzDm(fxkpOrderBO.getFppzDm())
                    .setYwlsh(fxkpOrderBO.getYwlsh())
                    .setZfkpqrfs(fxkpOrderBO.getZfkpqrfs())
                    .setZrrxm(fxkpOrderBO.getZrrxm())
                    .setZrrsfzjhm(fxkpOrderBO.getZrrsfzjhm())
                    .setZrrsjhm(fxkpOrderBO.getZrrsjhm())
                    .setZrryhzh(fxkpOrderBO.getZrryhzh())
                    .setKhyhszsf(fxkpOrderBO.getKhyhszsf())
                    .setJazslxDm(fxkpOrderBO.getJazslxDm())
                    .setSfcdf(fxkpOrderBO.getSfcdf())
                    .setFpbz(fxkpOrderBO.getFpbz())
                    .setKptzhddz(fxkpOrderBO.getKptzhddz())
                    .setZftzhddz(fxkpOrderBO.getZftzhddz())
                    .setCjr(appid)
                    .setCjsj(new Date()));
        }
        return map;
    }

    @Override
    @Transactional
    public FxkpOrderResultBO mtCreateOrder(FxkpOrderBO fxkpOrderBO) {
        //调用支付即开票平台
        FxkpOrderResultBO bo= zfjkpPtCommonClient.mtCreateOrder(fxkpOrderBO);
        if(!Objects.equals(bo.getCode(), "0"))throw new ServiceException(bo.getMsg());
        String appid=SessionUtils.getAppid();
        String dduuid = GyUtils.getUuid();
        List<YwddmxBO> ywddmx = fxkpOrderBO.getYwddmx();
        for (YwddmxBO ywddmxBO : ywddmx) {
            zfjkpFxkpDdmxbService.saveOrUpdate(new ZfjkpFxkpDdmxbDO()
                    .setUuid(GyUtils.getUuid())
                    .setDduuid(dduuid)
                    .setXmmc(ywddmxBO.getXmmc())
                    .setSphfwssflhbbm(ywddmxBO.getSphfwssflhbbm())
                    .setGgxh(ywddmxBO.getGgxh())
                    .setDw(ywddmxBO.getDw())
                    .setSl(ywddmxBO.getSl())
                    .setDj(ywddmxBO.getDj())
                    .setSlv(ywddmxBO.getSlv())
                    .setJe(ywddmxBO.getJe())
                    .setCjr(appid)
                    .setCjsj(new Date())
            );
        }
        ZfpzxxBO zfpzxxBO= fxkpOrderBO.getZfpzxx();
        zfjkpFxkpZfpzxxbService.saveOrUpdate(new ZfjkpFxkpZfpzxxbDO()
                .setUuid(GyUtils.getUuid())
                .setDduuid(dduuid)
                .setJylsh(zfpzxxBO.getJylsh())
                .setJyrq(zfpzxxBO.getJyrq())
                .setPcjylsh(zfpzxxBO.getPcjylsh())
                .setZzje(zfpzxxBO.getZzje())
                .setFkfyhbm(zfpzxxBO.getFkfyhbm())
                .setFkfyhmc(zfpzxxBO.getFkfyhmc())
                .setFkfyhzh(zfpzxxBO.getFkfyhzh())
                .setSkfyhmc(zfpzxxBO.getSkfyhmc())
                .setSkfyhzh(zfpzxxBO.getSkfyhzh())
                .setCjr(appid)
                .setCjsj(new Date())
        );
        this.saveOrUpdate(new ZfjkpFxkpDdxxbDO()
                .setUuid(dduuid)
                .setShbdbh(fxkpOrderBO.getShbdbh())
                .setKpybdbh(fxkpOrderBO.getKpybdbh())
                .setYwlx(fxkpOrderBO.getYwlx())
                .setFppzDm(fxkpOrderBO.getFppzDm())
                .setYwlsh(fxkpOrderBO.getYwlsh())
                .setZfkpqrfs(fxkpOrderBO.getZfkpqrfs())
                .setZrrxm(fxkpOrderBO.getZrrxm())
                .setZrrsfzjhm(fxkpOrderBO.getZrrsfzjhm())
                .setZrrsjhm(fxkpOrderBO.getZrrsjhm())
                .setZrryhzh(fxkpOrderBO.getZrryhzh())
                .setFpbz(fxkpOrderBO.getFpbz())
                .setKptzhddz(fxkpOrderBO.getKptzhddz())
                .setDdbh(bo.getDdbh())
                .setCjr(appid)
                .setCjsj(new Date()));
        return bo;
    }

    @Override
    @Transactional
    public Boolean payAndKpSubmit(PayAndKpSubmitBO payAndKpSubmitBO) {
        //调用支付即开票平台
        SimpleBooleanResponse result= zfjkpPtCommonClient.payAndKpSubmit(payAndKpSubmitBO);
        if(!Objects.equals(result.getCode(), "0"))throw new ServiceException(result.getMsg());
        return result.getData();
    }

    @Override
    @Transactional
    public TaxQrCodeBO taxQrCode(FxOrderQueryBO param) {
        //调用支付即开票平台
        TaxQrCodeBO result= zfjkpPtCommonClient.taxQrCode(param);
        if(!Objects.equals(result.getCode(), "0"))throw new ServiceException(result.getMsg());
        return result;
    }

    @Override
    @Transactional
    public Boolean orderCancel(FxOrderCancelBO fxOrderCancelBO) {
        SimpleBooleanResponse result= zfjkpPtCommonClient.orderCancel(fxOrderCancelBO);
        if(!Objects.equals(result.getCode(), "0"))throw new ServiceException(result.getMsg());
        if (result.getData()){
            ZfjkpFxkpDdxxbDO ddxx = this.lambdaQuery()
                    .eq(ZfjkpFxkpDdxxbDO::getDdbh,fxOrderCancelBO.getDdbh())
                    .eq(ZfjkpFxkpDdxxbDO::getShbdbh,fxOrderCancelBO.getShbdbh())
                    .one();
            ddxx.setDdzt("61");//已取消
            String appid = SessionUtils.getAppid();
            ddxx.setXgr(appid);
            ddxx.setXgsj(new Date());
            this.updateById(ddxx);
        }
        return result.getData();
    }

    @Override
    @Transactional
    public FxOrderStatusVO fxOrderStatus(FxOrderStatusReq req) {
        //调用支付即开票平台
        FxOrderStatusResponse response= zfjkpPtCommonClient.orderStatus(req);
        if(!Objects.equals(response.getCode(), "0"))throw new ServiceException(response.getMsg());
        String appid=SessionUtils.getAppid();
        ZfjkpFxkpDdxxbDO ddxxbDO = this.lambdaQuery()
                .eq(!BaseUtil.isNull(req.getDdbh()),ZfjkpFxkpDdxxbDO::getDdbh,req.getDdbh())
                .eq(!BaseUtil.isNull(req.getYwlsh()),ZfjkpFxkpDdxxbDO::getYwlsh,req.getYwlsh())
                .eq(ZfjkpFxkpDdxxbDO::getShbdbh,req.getShbdbh())
                .eq(ZfjkpFxkpDdxxbDO::getYwlx,req.getYwlx())
                .one();
        ddxxbDO.setDdzt(response.getDdzt());
        ddxxbDO.setSbyy(response.getSbyy());
        ddxxbDO.setXgr(appid);
        ddxxbDO.setXgsj(new Date());
        this.updateById(ddxxbDO);
        //组装返回参数
        FxOrderStatusVO resultBO = new FxOrderStatusVO();
        BeanUtils.copyProperties(response,resultBO);
        return resultBO;
    }

    @Override
    @Transactional
    public PcOrderStatusBO orderStatusBatch(PcOrderQueryBO param) {
        PcOrderStatusResponse response= zfjkpPtCommonClient.orderStatusBatch(param);
        if(!Objects.equals(response.getCode(), "0"))throw new ServiceException(response.getMsg());
        List<JgmxBO> jgmx = response.getJgmx();
        Map<String,JgmxBO> jgmxMap = jgmx.stream()
                .collect(Collectors.toMap(JgmxBO::getDdbh, bo -> bo));
        List<String> cgDsbh = jgmx.stream().filter(j->j.getJgbs()==0).map(JgmxBO::getDdbh).collect(Collectors.toList());
        List<String> sbDsbh = jgmx.stream().filter(j->j.getJgbs()!=0).map(JgmxBO::getDdbh).collect(Collectors.toList());

        List<ZfjkpFxkpDdxxbDO> cgDdxxList = this.lambdaQuery()
                .eq(ZfjkpFxkpDdxxbDO::getPch,param.getPch())
                .in(ZfjkpFxkpDdxxbDO::getDdbh,cgDsbh).list();
        List<ZfjkpFxkpDdxxbDO> sbDdxxList = this.lambdaQuery()
                .eq(ZfjkpFxkpDdxxbDO::getPch,param.getPch())
                .in(ZfjkpFxkpDdxxbDO::getDdbh,sbDsbh).list();

        for (ZfjkpFxkpDdxxbDO ddxxbDO : cgDdxxList) {
            JgmxBO jgmxBO = jgmxMap.get(ddxxbDO.getDdbh());
            ddxxbDO.setJgbs("0");
            String appid = SessionUtils.getAppid();
            ddxxbDO.setXgr(appid);
            ddxxbDO.setXgsj(new Date());
            List<SfmxBO> sfmx= jgmxBO.getSkxx();
            List<ZfjkpFxkpSkxxbDO> skxxbDOS = new ArrayList<>();
            for (SfmxBO sfmxBO : sfmx) {
                ZfjkpFxkpSkxxbDO skxxbDO = new ZfjkpFxkpSkxxbDO();
                skxxbDO.setUuid(GyUtils.getUuid());
                skxxbDO.setDduuid(ddxxbDO.getUuid());
                skxxbDO.setYbtse(sfmxBO.getYbtse());
                skxxbDO.setZsxmDm(sfmxBO.getZsxmDm());
                skxxbDO.setZspmDm(sfmxBO.getZspmDm());
                skxxbDO.setZszmDm(sfmxBO.getZszmDm());
                skxxbDO.setSkssqq(sfmxBO.getSkssqq());
                skxxbDO.setSkssqz(sfmxBO.getSkssqz());
                skxxbDO.setCjr(appid);
                skxxbDO.setCjsj(new Date());
                skxxbDOS.add(skxxbDO);
            }
            zfjkpFxkpSkxxbService.saveBatch(skxxbDOS);
        }
        this.updateBatchById(cgDdxxList);

        for (ZfjkpFxkpDdxxbDO ddxxbDO : sbDdxxList) {
            JgmxBO jgmxBO = jgmxMap.get(ddxxbDO.getDdbh());
            ddxxbDO.setJgbs(String.valueOf(jgmxBO.getJgbs()));
            ddxxbDO.setSbyy(jgmxBO.getSbyy());
            String appid = SessionUtils.getAppid();
            ddxxbDO.setXgr(appid);
            ddxxbDO.setXgsj(new Date());
        }
        this.updateBatchById(sbDdxxList);

        PcOrderStatusBO pcOrderStatusBO = new PcOrderStatusBO();
        BeanUtil.copyProperties(response, pcOrderStatusBO);
        List<JgmxBO> jgmxBOS = new ArrayList<>(response.getJgmx());
        pcOrderStatusBO.setJgmx(jgmxBOS);
        return pcOrderStatusBO;
    }

}




